const path = require('path')
const {
	sleepRandom,
	getWindow,
	getMaxResponse,
	getResponse,
	getMSTime,
} = require('../utils')
const _CrawlerMongo = require('../utils/config/CrawlerMongo')
const _mongo = new _CrawlerMongo()

const _Task = require('../utils/dao-mongo/Task')

const _TYPE = [{
	id: 'book',
	name: '图书资源',
}, {
	id: 'datum',
	name: '资料资源',
}, {
	id: 'music',
	name: '音乐资源',
}, {
	id: 'software',
	name: '软件资源',
}, {
	id: 'edu',
	name: '教育资源',
}, {
	id: 'game',
	name: '游戏资源',
}, {
	id: 'tv',
	name: '综艺资源',
}, {
	id: 'cartoon',
	name: '动漫资源',
}]

const _typeUrl = 'http://verycd.gdajie.com/#{type}/page'
const _size = 20

async function run() {

	return initTask()
}

run().then(r => {
	console.log(`[${getMSTime()}]`, `[${process.pid}]`, '---r---', r)
	process.exit(0)
}).catch(err => {
	console.log(`[${getMSTime()}]`, `[${process.pid}]`, '---err---', err)
	process.exit(1)
})

async function initTask() {
	let _type_ = {
		id: 'gdajie',
		name: '逛大街抓取'
	}
	let _cate_ = {
		id: 'fetch',
		name: '爬虫',
	}
	await _Task.deleteMany({
		'_type_.id': _type_.id,
		'_cate_.id': _cate_.id,
	});
	for (let i = 0; i < _TYPE.length; i++) {
		let {
			id,
			name,
		} = _TYPE[i]
		let task_name = `${name}_${id}`
		let task = new _Task(task_name, _type_)
		let {
			amount,
			totalPage,
			currentPage,
		} = await getGdajieInfo(id)
		// console.log('---amount---', amount, totalPage)
		let query = {
			size: _size,
			typeId: id,
			typeName: name,
			totalPage,
		}
		await task.create({
			query,
			_type_,
			_cate_,
			_bash_: {
				child_process: 'fork', //exec, execFile, fork, spawn
				cmd: 'MiniGdajieTaskFactory.js',
				// cmd: `node match-${table}.js #{_id} #{init} > ../logs/match_${table}_#{_id}.log 2>&1`,
				// cmd: `node GdajieTaskFactory.js #{_id} #{init} > ../logs/ed2kers_#{_id}.log 2>&1`,
				options: {
					cwd: __dirname
				},
				params: {
					'#{_id}': '',
					'#{init}': ''
				},
				args: [],
			},
			pid: 0,
			num: 0,
			currentPage,
			total: amount,
			amount,
			status: 0, // 状态 (0: 待处理 1: 运行中 -1: 完成)
			create_time: getMSTime(),
			modify_time: getMSTime(),
		})
	}

}

async function getGdajieInfo(typeId) {
	await _mongo.setIndex(typeId, [{
		name: {
			id: 1,
		},
		opts: {
			unique: true
		}
	}])
	let typeUrl = _typeUrl.replace('#{type}', typeId)
	let window = await getMaxResponse(getWindow, typeUrl, {
		ms: 20000
		// ms: 0
	});
	let document = window.document
	let totalList = document.querySelectorAll('div#accordion a.menu')
	console.log('----totalList----', typeUrl)
	let pageInfoDiv = document.querySelector('div.pager')
	let arr = pageInfoDiv.querySelectorAll('div a')
	let totalPage = 0
	for (let i = 0; i < arr.length; i++) {
		let pageDiv = arr[i]
		let page = pageDiv.innerHTML
		page = parseInt(page.replace(/[^\d]/g, ''))
		if (page > 200) {
			totalPage = page
			break;
		}
	}
	let amount = 0
	for (let i = 0; i < totalList.length; i++) {
		let totalA = totalList[i]
		if (totalA.href.indexOf(typeId) > -1) {
			let html = totalA.innerHTML
			html = html.replace(/[^\d]/g, '')
			amount = parseInt(html)
		}
	}
	return {
		amount,
		totalPage,
		currentPage: 1,
	}
}